<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../../lib/layui-v2.8.12/src/layui.js"></script>
  <link rel="stylesheet" href="../../lib/layui-v2.8.12/src/css/layui.css">
  <script src="../../utils/form-serialize.js"></script>
  <script src="../../lib/axios/axios.min.js"></script>
  <link rel="stylesheet" href="../../css/common.css">

  <title>Document</title>
</head>

<body>
  <!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
  <form class="layui-form layui-form-pane" id="add" action="./addPlayer.php" method="POST">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">球员名</label>
        <div class="layui-input-block">
          <input type="text" name="playerName" maxlength="10" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">所在球队</label>
        <div class="layui-input-inline">
          <select id="selectTeam" name="teamId">
          </select>
        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">身高</label>
        <div class="layui-input-block" style="width: 100px;">
          <!-- 身高在150-220之间 -->
          <input type="text" name="height" pattern="^(1[5-9][0-9]|1[0-9]{2}|200|20[1-9]|21[0-9]|220)$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-inline" style="width: 100px;">
          <input type="text" name="age" pattern="^(1[8-9]|[2-4][0-9]|50)$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">号码</label>
        <div class="layui-input-inline" style="width: 100px;">
          <input type="text" name="num" pattern="^[0-9]{1,2}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">位置</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input name="pos" autocomplete="off" maxlength="8" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">年薪</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input name="salary" autocomplete="off" pattern="^\d{0,5}$" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">俱乐部</label>
        <div class="layui-input-inline">
          <input name="club" autocomplete="off" maxlength="10" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">出生日期</label>
      <div class="layui-input-block" style="width: 200px;">
        <input type="text" name="birth" autocomplete="off" class="layui-input" id="date" placeholder="yyyy-MM-dd">
      </div>
    </div>

    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">球员描述</label>
      <div class="layui-input-block">
        <textarea name="desc" maxlength="300" class="layui-textarea"></textarea>
      </div>
    </div>

    <div class="layui-form-item btn">
      <div>
        <button class="layui-btn" id="confirm">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <?php
  session_start();
  $user_id = $_SESSION['userId'];
  DEFINE('DB_USER', 'worldcup');
  DEFINE('DB_PASSWORD', '123456');
  DEFINE('DB_HOST', 'localhost');
  DEFINE('DB_NAME', 'worldcup');

  $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$dbc) {
    die('Could not to connect to Mysql:' . mysqli_connect_error());
  }
  mysqli_set_charset($dbc, 'utf8');

  $teamNameSql = "select team_name from team;";
  $teamAaary = mysqli_query($dbc, $teamNameSql);

  // 初始化一个空数组来存储所有行数据
  $dataArray = array();
  // 逐行提取数据并存入数组
  while ($row = mysqli_fetch_assoc($teamAaary)) {
    $dataArray[] = $row['team_name'];
  }
  ?>
  <script>
    let res = <?php echo json_encode($dataArray); ?>;
    var select = document.getElementById('selectTeam');
    for (var i = 1; i <= res.length; i++) {
      var option = document.createElement("option");
      option.setAttribute("value", i);
      option.innerText = res[i - 1];
      if (i == res.length) {
        option.setAttribute("selected", "selected");
      }
      select.appendChild(option)
    }
    //渲染下拉框
    layui.use(['laydate', 'form'], function() {
      var form = layui.form;
      form.render("select");
      form.on('select', function(data) {
        // console.log(data.value);
      });
      //日期选择器
      var laydate = layui.laydate;
      // 渲染
      laydate.render({
        elem: '#date'
      });
    });
  </script>

  <?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $playerName = $_POST['playerName'];
    $teamId = $_POST['teamId'];
    $salary = $_POST['salary'];
    $height = $_POST['height'];
    $age = $_POST['age'];
    $pos = $_POST['pos'];
    $num = $_POST['num'];
    $club = $_POST['club'];
    $desc = $_POST['desc'];
    $birth = $_POST['birth'];

    $sqlAdd = "insert into player(player_name,team_id,player_age,height,player_pos,player_desc,player_salary,player_club,birth_date,player_num,createdby) values('{$playerName}',$teamId,$age,$height,'{$pos}','{$desc}',$salary,'{$club}','{$birth}',$num,$user_id);";
    $resAdd = mysqli_query($dbc, $sqlAdd);
    echo '<script>';
    if ($resAdd == 1) {
      echo '
        layui.use(\'layer\', function() {
          var layer = layui.layer;
          layer.msg(\'添加球员成功\');
          setTimeout(() => {
            var index = parent.layer.getFrameIndex(window.name); // 获取弹出层索引
            parent.layer.close(index); // 关闭弹出层
          }, 2000);
        });
      ';
    } else {
      echo '
        layui.use(\'layer\', function() {
          var layer = layui.layer;
          layer.msg(\'添加球员失败,请检查参数\');
          setTimeout(() => {
            var index = parent.layer.getFrameIndex(window.name); // 获取弹出层索引
            parent.layer.close(index); // 关闭弹出层
          }, 2000);
        });      
      ';
    }
    echo '</script>';
  }
  ?>
</body>

</html>